package com.bbs.toutiao.service.client.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.emoji.EmojiUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.bbs.toutiao.aspect.SysLog;
import com.bbs.toutiao.exception.BaseException;
import com.bbs.toutiao.exception.ExceptionEnum;
import com.bbs.toutiao.mapper.admin.CommentMapper;
import com.bbs.toutiao.mapper.clinet.ClientArticleEditMapper;
import com.bbs.toutiao.mapper.clinet.ClientUserMapper;
import com.bbs.toutiao.service.client.CommentEditService;
import com.bbs.toutiao.vo.article.ArticleVo;
import com.bbs.toutiao.vo.client.AddCommentVo;
import com.bbs.toutiao.vo.client.CommentClinetVo;
import com.bbs.toutiao.vo.client.GetCommentByUserIdVo;
import com.bbs.toutiao.vo.comm.PageVo;
import com.bbs.toutiao.vo.comment.CommentPageRequestVo;
import com.vdurmont.emoji.Emoji;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 描述:
 * 作者： zhangBin
 * 时间： 2023/10/11 8:35
 * 版本： 1.0
 */

@Slf4j
@Service
public class CommentEditServiceImpl implements CommentEditService {

    @Resource
    CommentMapper commentMapper;
    @Resource
    ClientUserMapper clientUserMapper;

    @Resource
    ClientArticleEditMapper editMapper;


    /**
     * 客户端查询评论
     *
     * @param requestVo
     * @return
     */
    @Override
    public PageVo<CommentClinetVo> findByCommentPageClient(CommentPageRequestVo requestVo) {
        List<CommentClinetVo> entityList = commentMapper.findByCommentPageClient(requestVo);
        int sum = commentMapper.sumCountByArticleId(requestVo.getArticleId());
        for (CommentClinetVo entity : entityList) {
            entity.setContent(EmojiUtil.toUnicode(entity.getContent()));
        }
        PageVo<CommentClinetVo> pageVo = new PageVo<>();
        List<CommentClinetVo> client = newsList(entityList, 0);
        pageVo.setPageList(client);
        pageVo.setTotal(sum);
        return pageVo;
    }

    //递归查询
    public List<CommentClinetVo> newsList(List<CommentClinetVo> list, int pradId) {
        List<CommentClinetVo> clientVos = new ArrayList<>();
        for (CommentClinetVo clientVo : list) {
            if (clientVo.getParentId() == pradId) {
                List<CommentClinetVo> children = newsList(list, clientVo.getId());
                clientVos.add(clientVo);
                clientVo.setChildren(children);
            }
        }
        return clientVos;
    }


    /**
     * 点赞
     *
     * @param comment
     * @return
     */
    @SysLog("用户对评论点赞")
    @Transactional
    @Override
    public boolean addCountByCommentId(Map<String, Integer> comment) {
        int i = 0;
        int s = 0;
        try {
            i = commentMapper.updateCount(comment);
            if (comment.get("type") == 1) {
                //增加数据
                s = commentMapper.insertUserLike(comment);
            } else {
                //删除数据
                s = commentMapper.deleteUserLike(comment);
            }
        } catch (Exception e) {
            log.error(e + ":" + "----" + comment);
            throw new BaseException(ExceptionEnum.INTERNAL_SERVER_ERROR);
        }
        return i == s;
    }

    /**
     * 新增评论
     *
     * @param commentVo
     * @return
     */
    @SysLog("用户发表评论")
    @Override
    public int addComment(AddCommentVo commentVo) {
        Date now = new Date();
        commentVo.setCreatedAt(DateUtil.format(now,"yyyy-MM-dd HH:mm:ss"));
        return commentMapper.insertComment(commentVo);
    }

    /**
     * 查询指定用户分页的评论
     *
     * @param vo
     * @return
     */
    @SysLog("用户查看自己的评论信息")
    @Override
    public Map<String, Object> getCommentByUserIdPage(CommentPageRequestVo vo) {
        List<GetCommentByUserIdVo> commetnList = clientUserMapper.getCommentByUserId(vo);
        if (commetnList.isEmpty()) {
            throw new BaseException(ExceptionEnum.NOT_FOUND);
        }
        List<Integer> its = new ArrayList<Integer>();
        for (GetCommentByUserIdVo ar : commetnList) {
            if (ar.getImg() != null && !ar.getImg().equals(" ")) {
                String content = EmojiUtil.toUnicode(ar.getContent());
                ar.setContent(content);

                //将json字符串转为数组
                JSONArray objects = JSONUtil.parseArray(ar.getImg());
                List<String> list = JSONUtil.toList(objects, String.class);
                if (list.size() > 1) {
                    ar.setImages(CollectionUtil.sub(list, 0, 1));
                }
                its.add(ar.getArticleId());
                ar.setImg(null);
            }
        }
        //根据文章id查询文章信息
        List<ArticleVo> articleVos = editMapper.articleListByCommentsUserId(its, vo.getUserId());
        long count = clientUserMapper.getCommentCountByUserId(vo);
        //设置分页信息
        PageVo<GetCommentByUserIdVo> pageVo = new PageVo<>();
        pageVo.setPageList(commetnList);
        pageVo.setTotal(count);
        pageVo.setSize(vo.getSize());
        pageVo.setPage(vo.getPage());
        Map<String, Object> map = new HashMap<>();
        map.put("pageVo", pageVo);
        map.put("articleVos", articleVos);
        return map;
    }


    @SysLog("用户删除评论")
    @Transactional
    @Override
    public boolean deleteComment(Integer id,Integer par) {

        int i = clientUserMapper.deleteComment(id);
        clientUserMapper.updateParentId(id,par);
        return i > 0;
    }

}
